$(function () {
    obj.init();
});

var obj = new function() {
    'use strict';
    return {
        form: null,
        params: {
            schoolId: null,
            classId: null
        },
        dom: {
            save: null,
            subject: null,
            standard: null
        },
        subjectHtml: {},
        init: function () {
            this.params.schoolId = xhTool.getQuery('schoolId');
            this.params.classId  = xhTool.getQuery('classId');

            this.dom.save       = window.top.xhTop.getModalConfirmDom();
            this.dom.subject    = $('#sSubject');
            this.dom.standard   = $('.standard-div');

            this.form = new xhForm($('#xh_form'));
            this.form.parse().fill(groupInfo || {iSchoolId: this.params.schoolId, sClassId: this.params.classId});

            this.listener();
        },
        listener: function () {
            var $this = this;
            //保存按钮
            $this.dom.save.click(function () {
                $this.funObj().saveBrushGroup();
            });
            //切换科目
            $this.funObj().dealSubjectDom();
            $this.dom.subject.change(function () {
                 $this.funObj().changeSubject($(this).val());
            }).change();
        },
        funObj: function () {
            var $this = this;
            return {
                saveBrushGroup: function () {
                    $this.form.submit('stats_brush/saveBrushGroup', {}, function (data) {
                         if (xhExtra.checkError(data)) {
                             xhExtra.setBtnNormal($this.dom.save);
                             return false;
                         }
                         if ($this.params.classId) {
                             // window.top.xhTop.getActiveFrame().obj.groupGrid.treegrid('reload', $this.params.classId);
                             window.top.xhTop.getActiveFrame().obj.funObj().reloadGroupGrid($this.params.classId);
                         }
                         window.top.xhTop.closeModal();
                    }, {check: false});
                },
                dealSubjectDom: function () {
                    $.each($('.subject'), function (index, dom) {
                        $this.subjectHtml[$(dom).attr('data-name')] = dom;
                        // $(dom).remove();
                    });
                },
                changeSubject: function (name) {
                    $this.dom.standard.html($this.subjectHtml[name]);
                    // if (groupInfo) {
                    //     $this.form.fill(groupInfo);
                    // }
                    $this.form.bindRequired($this.dom.save);
                }
            }
        }
    }
};